package com.example.apiproject.controller;

import com.example.apiproject.entity.Groups;
import com.example.apiproject.service.GroupsService;
import com.example.apiproject.utils.Res;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Groups)表控制层
 *
 * @author makejava
 * @since 2023-09-23 13:40:57
 */
@RestController
@RequestMapping("groups")
public class GroupsController {
    /**
     * 服务对象
     */
    @Resource
    private GroupsService groupsService;


    /**
     * 通过用户ID查询用户加入的群组
     * @param userId 筛选条件
     */
    @GetMapping("queryByUserId")
    public Res<List<Groups>> queryByUserId(String userId) {
        return Res.ok(this.groupsService.queryByUserId(userId));
    }
    /**
     * 分页查询
     *
     * @param groups 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<Groups>> queryByPage(Groups groups, PageRequest pageRequest) {
        return ResponseEntity.ok(this.groupsService.queryByPage(groups, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Groups> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.groupsService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param groups 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<Groups> add(Groups groups) {
        return ResponseEntity.ok(this.groupsService.insert(groups));
    }

    /**
     * 编辑数据
     *
     * @param groups 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<Groups> edit(Groups groups) {
        return ResponseEntity.ok(this.groupsService.update(groups));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.groupsService.deleteById(id));
    }

}

